import {reqAddOrUpdateShopCart, reqGetDetailInfo} from "@/api";
//Home模块
const state = {
    detailInfo: {}
}

const actions = {
    async getDetailInfo({commit}, skuId) {
        let result = await reqGetDetailInfo(skuId)
        if (result.code === 200) {
            commit('GETDETAILINFO', result.data)
        }
    },
    async addOrUpdateShopCart({commit}, {skuId, skuNum}) {
        let result = await reqAddOrUpdateShopCart(skuId, skuNum)
        //返回一个promise对象
        if (result.code === 200) {
            return 'success'
        } else {
            return Promise.reject(new Error('fail to addOrUpdateShopCart'))
        }
    }
}

const mutations = {
    GETDETAILINFO(state, detailInfo) {
        state.detailInfo = detailInfo
    }
}

const getters = {
    categoryView(state) {
        return state.detailInfo.categoryView || {}
    },
    skuInfo(state) {
        return state.detailInfo.skuInfo || {}
    },
    spuSaleAttrList(state) {
        return state.detailInfo.spuSaleAttrList || {}
    },

}//为了简化仓库中的数据

export default {
    namespaced: true,
    state,
    actions,
    mutations,
    getters
}

